package com.petadoption.serverclient.controller;

import com.petadoption.serverclient.service.IProductDescrService;
import com.petadoption.serverclient.service.IProductService;
import com.petadoption.serverclient.service.IProductSwipeService;
import com.petadoption.serverclient.utils.Result;
import com.petadoption.serverclient.vo.ProductSwipeVO;
import com.petadoption.serverclient.vo.ProductVO;
import com.petadoption.serverclient.vo.ProductdescrVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品前端控制器
 * </p>
 *
 * @author 汪仕杰
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/shopping")
public class ProductController {

    @Resource
    private IProductService productService;

    @Resource
    private IProductDescrService productDescrService;

    @Resource
    private IProductSwipeService productSwipeService;

    @Resource
    private Result result;

    @GetMapping("/all")
    public Result findProductAll() {
        List<ProductVO> productAll = productService.findProductAll();
        if (productAll.isEmpty()) return result.fail("暂无商品");
        return result.success("所有商品", productAll);
    }

    @GetMapping("/search")
    public Result findProduct(String parameter) {
        List<ProductVO> productAll = productService.findProduct(parameter);
        if (productAll.isEmpty()) return result.fail("暂无商品");
        return result.success("商品列表", productAll);
    }

    @GetMapping("/{productId}")
    public Result findProductById(@PathVariable Integer productId) {
        ProductVO product = productService.findProductById(productId);
        List<ProductdescrVO> descrList = productDescrService.findProductDescrById(productId);
        List<ProductSwipeVO> swipeList = productSwipeService.findProductSwipeById(productId);
        product.setProductSwipe(swipeList);
        product.setProductDescr(descrList);
        if (product != null) return result.success("当前商品", product);
        return result.fail("暂无商品");
    }

}
